<?php

namespace services\task;

use Yii;
use common\components\Service;
use common\models\member\Member;
use common\models\task\TaskMember;

/**
 * Class TaskMemberService
 * @package services\task
 */
class TaskMemberService extends Service
{
    /**
     * 任务人列表
     *
     * @param array $params
     * @return array
     */
    public function getTaskMemberList(array $params = []): array
    {
        $taskMemberList = TaskMember::find()->alias('tm')
            ->innerJoin(Member::tableName().' m', 'm.id = tm.member_id')
            ->select(['tm.task_id', 'GROUP_CONCAT(m.realname) AS member_names'])
            ->where([
                'tm.task_id' => $params['task_id'],
            ])
            ->indexBy('task_id')
            ->groupBy('tm.task_id')
            ->asArray()
            ->all();

        foreach ($taskMemberList as $key => $row) {
            $taskMemberList[$key]['member_name_arr'] = explode(',', $row['member_names']);
        }

        return $taskMemberList;
    }
}
